import db from "../models/index.js";
import {Op, Sequelize} from "sequelize";
import {getNow} from "../utils/utils.js";

let userBase = db.user_base;
let userBaseDao = {};
//判断是否存在这个用户信息
userBaseDao.isExisted = (_account) => {
  return userBase.findOne({
    where: {
      _account
    },
    raw: true,
  });
};
//获取用户数据
userBaseDao.getUserBaseInfo = (_account) => {
  return userBase.findOne({
    where: {
      _account
    },
  });
};
//获取用户数据--数组
userBaseDao.getUserBaseInfoList = (_accountList, startTime = 0) => {
  return userBase.findAll({
    where: {
      _account: {
        [Op.in]: _accountList,
      },
      _update_time: {
        [Op.gte]: startTime
      }
    }
  });
};
//添加用户信息
userBaseDao.addUserBaseInfo = (_account, _avatar, _birth, _signature, _name, t) => {
  let _update_time = getNow();
  if (!t) {
    return userBase.create({_account, _avatar, _birth, _signature, _name, _update_time});
  }
  return userBase.create({_account, _avatar, _birth, _signature, _name, _update_time}, {transaction: t});
};
//更新用户信息
userBaseDao.updateUserBaseInfo = (_account, _avatar, _birth, _signature, _name, t) => {
  let _update_time = getNow();
  if (!t) {
    return userBase.update({_avatar, _birth, _signature, _name, _update_time}, {where: {_account}});
  }
  return userBase.update({_avatar, _birth, _signature, _name, _update_time}, {where: {_account}, transaction: t});
};
export default userBaseDao;
